<template>
	<div>
		<div class="bannerBox"><image :src="banner" mode="widthFix"></image></div>
		<div class="container">
			<div class="searchBox">
				<u-subsection :list="tabList" :current="curNow" @change="sectionChange" activeColor="#f56c6c"></u-subsection>
				<div class="searchTj" v-if="curNow == 0">
					<div class="s_inp">
						<u--input placeholder="请输入单位名称查询面试形式" border="surround" v-model="searchDwmc" @change="changeDwmc"></u--input>
						<p class="errorText">* 支持模糊搜索(如输入公安局)</p>
					</div>
				</div>
				<div class="searchTj" v-else>
					<div class="s_inp">
						<u-cell-group><u-cell icon="map" :title="selectMsxs" @click="p_msxs = true" :isLink="true" arrow-direction="right"></u-cell></u-cell-group>
						<u-picker
							:show="p_msxs"
							:columns="msxsList"
							@confirm="changeMsxs"
							@cancel="closePicker"
							@close="closePicker"
							:closeOnClickOverlay="true"
							:immediateChange="true"
						></u-picker>
						<p class="errorText">* 黑龙江地区大部分岗位进行结构化面试，仅有少部分岗位进行结构化小组</p>
					</div>
				</div>

				<div class="s_btn"><u-button type="primary" @click="search()" color="#FF805A">立即查询</u-button></div>
				<p class="errorText">* 数据基础为2022年度官方公布</p>
			</div>

			<div class="resultBox" v-if="showResult">
				<div class="container" v-if="resultList.length > 0">
					<h4>
						为您扫描到
						<i>{{ resultList.length }}</i>
						个结果
					</h4>
					<ul class="pagination" v-if="maxPage > 1">
						<li @click="selectPage(1)">首页</li>
						<li @click="prevPage">上一页</li>
						<li>
							<span>{{ page }}/{{ maxPage }}</span>
						</li>
						<li @click="nextPage">下一页</li>
						<li @click="selectPage(maxPage)">尾页</li>
					</ul>
					<div class="items">
						<div class="item" v-for="(item, index) in resultList" :key="index" v-if="index < 5 * page && index >= 5 * page - 5">
							<h3>{{ item.msfs }}</h3>
							<p>
								<span>单位名称</span>
								{{ item.bmmc }}
							</p>
							<p>
								<span>面试考区</span>
								<i>{{ item.mskq }}</i>
							</p>
							<p>
								<span>进面分数</span>
								<u-button type="primary" @click="getFs(item)" color="#FF805A" :customStyle="style3" size="mini">立即查询</u-button>
							</p>
						</div>
					</div>
					<ul class="pagination" v-if="maxPage > 1">
						<li @click="selectPage(1)">首页</li>
						<li @click="prevPage">上一页</li>
						<li>
							<span>{{ page }}/{{ maxPage }}</span>
						</li>
						<li @click="nextPage">下一页</li>
						<li @click="selectPage(maxPage)">尾页</li>
					</ul>
				</div>
				<div class="container" v-else>
					<h4>
						<i>
							未检索到单位信息
							<br />
							请修改条件后重新检索
						</i>
					</h4>
				</div>
			</div>

			<div class="searchDetail">
				<u-popup :show="showFs" mode="center" @close="closePicker" :closeable="true">
					<view>
						<div class="popcontent">
							<u-divider text="面试分数详情" textPosition="left"></u-divider>
							<div class="item" v-for="(item, index) in fsList" :key="index">
								<ul>
									<li>
										<span>单位名称</span>
										{{ item.danwei_name }}
									</li>
									<li>
										<span>用人处室</span>
										{{ item.yongrenks }}
									</li>
									<li>
										<span>职位名称</span>
										{{ item.zhiwei_name }}
									</li>
									<li>
										<span>进面分数</span>
										<i>{{ item.fsx ? item.fsx : '未公布' }}</i>
									</li>
								</ul>
								<u-line dashed></u-line>
							</div>
						</div>
					</view>
				</u-popup>
			</div>

			<!-- <div class="addFriend">
				<div class="container"><image :src="userInfo.ewm" show-menu-by-longpress="true" mode="widthFix"></image></div>
			</div> -->

			<u-modal
				:show="showLogin" 
				title="体验次数已用尽,请输入口令码" 
				:cancelText="'暂不登录'" 
				:showCancelButton="false"
				:showConfirmButton="false" 
				>
				<view class="slot-confirmButton">
					<div class="modalBtns">
						<div>
							<u--input
							    placeholder="请输入口令码"
							    border="surround"
							    v-model="klcode"
							  ></u--input>
						</div>
						<u-button type="error" text="确定" @click="checkKlm"></u-button>
						<div class="ewmBox">
							<image :src="userInfo.ewm"show-menu-by-longpress="true" mode="widthFix"></image>
							<p>长按识别二维码<br>添加助教微信领取口令码</p>
						</div>
					</div>
					
				</view>
			</u-modal>

			<div class="name">{{ name }}</div>
			<share></share>
			<u-toast ref="uToast"></u-toast>
		</div>
	</div>
</template>

<script>
import { mapActions } from 'vuex';
import share from '../../common/share/index.vue';
const db = wx.cloud.database()
export default {
	data() {
		return {
			klcode: "",
			banner: 'http://www.baixuemianshi.com/zhuanti/images/banner_msxs.jpg',
			name: '',
			searchNum: 0,
			userInfo: {
				ewm: ''
			},
			tabList: ['单位查询面试形式', '面试形式查询单位'],
			curNow: 0,
			resultList: [],
			searchDwmc: '',
			page: 1,
			maxPage: '',
			showResult: false,
			scrollTop: 0,
			style3: {
				width: '45%'
			},
			showLogin: false,
			showFs: false,
			fsList: [],

			msxsList: [],
			selectMsxs: '请选择面试形式',
			p_msxs: false
		};
	},
	onShareAppMessage(options) {
		let shareObj = {
			title: '省考-面试形式查询',
			path: '/pages/sk-tools/msxs/index?name=' + this.name,
			imageUrl: ''
		};
		return shareObj;
	},
	onShareTimeline(res) {
		let that = this;
		return {
			title: '省考-面试形式查询',
			query: 'name=' + that.name
		};
	},
	onShow() {},
	onPullDownRefresh() {
		console.log('refresh');
		setTimeout(function() {
			uni.stopPullDownRefresh();
		}, 1000);
	},
	onPageScroll(e) {
		let that = this;
		that.scrollTop = e.scrollTop;
	},
	onLoad(options) {
		if (!options) {
		} else {
			this.name = options.name;
		}
	},
	computed: {
		searchtimes() {
			return this.$store.getters.searchtimes ? this.$store.getters.searchtimes : '';
		},
		userphone() {
			return this.$store.getters.userphone ? this.$store.getters.userphone : '';
		},
		useropenid() {
			return this.$store.getters.useropenid ? this.$store.getters.useropenid : '';
		},
		username() {
			return this.$store.getters.username ? this.$store.getters.username : '';
		},
		loginpage() {
			return this.$store.getters.loginpage ? this.$store.getters.loginpage : '';
		}
	},
	mounted() {
		if (this.searchtimes > 0) {
			this.searchNum = this.searchtimes
		}
		this.name = this.name ? this.name : this.username;
		this.LogSetName(this.name);
		this.getewm()
		let hLogin = wx.getStorageSync("sk_bl_hasLogin");
		if(hLogin){
			this.sk_bl_hasLogin = true
		}
	},
	watch: {
		userphone(val) {
			if (this.userphone) {
				if (!this.loginpage.includes('sk-msxs')) {
					this.singleRegister();
				}
			}
		}
	},
	created() {
		let that = this;
		that.getMsxsList();
	},
	methods: {
		...mapActions(['LogSetSearchtimes', 'LogSetPhone', 'LogSetOpenid', 'LogSetName', 'LogSetPage']),
		getopenid() {
			let that = this;
			wx.cloud.callFunction({
				name: 'getopenid',
				data: {},
				complete: res => {
					that.openid = res.result.openid
				}
			});
		},
		getewm(){
			let that = this
			db.collection("bxms_ewm").where({
			    name: that.name
			}).get({
				success: function(res){
					that.userInfo.ewm = res.data[0].ewm1
					that.userInfo.name = that.name
					
					db.collection('23gkbl_zwsc_user').add({
						data: {
							openid: openid,
							name: that.name,
							time: new Date()
						},
						success: function(res) {
							console.log('储存成功');
						}
					});
					
				},
				fail: function(req){
					console.log(req)
				}
			})
		},
		checkKlm(){
			let that = this
			wx.cloud.callFunction({
				name: 'get_klm',
				data: {},
				complete: res => {
					console.log('callFunction result: ', res)
					if(res.result.klm == that.klcode){
						that.sk_bl_hasLogin = true
						that.showLogin = false
						wx.setStorageSync('sk_bl_hasLogin', true);
					}else{
						console.log("口令码不正确")
						uni.showToast({
							icon: "error",
							title: '口令码不正确'
						});
						that.showLogin = true
					}
				}
			})
		},
		closePicker() {
			this.showFs = false;
			this.showLogin = false;
			this.p_msxs = false;
		},
		changeDwmc(e) {
			let that = this;
			// that.searchDwmc = e
		},
		sectionChange(index) {
			this.curNow = index;
		},
		getMsxsList() {
			let that = this;

			uni.request({
				url: 'https://hlj.offcn.com/zg/lynn/msxs2022.js',
				method: 'get',
				dataType: 'text',
				data: {
					sstimes: new Date()
				},
				success: function(res) {
					var resjson = JSON.parse(res.data);
					let l = resjson.map(item => item.msfs);
					l = [...new Set(l)];
					that.msxsList[0] = l;
				}
			});
		},
		changeMsxs(e) {
			let that = this;
			that.selectMsxs = e.value[0];
			that.p_msxs = false;
		},
		search() {
			let that = this;
			if(that.searchNum >= 3 && !that.sk_bl_hasLogin){
				that.showLogin = true
				return 
			}
			if (that.curNow == 1) {
				that.searchMsxs();
				return;
			}

			if (!that.searchDwmc) {
				that.showToast('error', '请输入单位名称');
				return;
			}
			uni.request({
				url: 'https://hlj.offcn.com/zg/lynn/msxs2022.js',
				method: 'get',
				dataType: 'text',
				data: {
					sstimes: new Date()
				},
				success: function(res) {
					var resjson = JSON.parse(res.data);
					that.filterDw(resjson);
				}
			});
		},
		searchMsxs() {
			let that = this;
			uni.request({
				url: 'https://hlj.offcn.com/zg/lynn/msxs2022.js',
				method: 'get',
				dataType: 'text',
				data: {
					sstimes: new Date()
				},
				success: function(res) {
					var resjson = JSON.parse(res.data);
					that.filterDw2(resjson);
				}
			});
		},
		filterDw(info) {
			let that = this;
			let arrs = info.filter(item => item.bmmc.indexOf(that.searchDwmc) != -1);
			that.resultList = arrs;
			that.page = 1;
			that.maxPage = Math.ceil(that.resultList.length / 5);
			that.showResult = true;
			if (!that.userphone) {
				that.searchNum += 1;
				that.LogSetSearchtimes(that.searchNum);
			}
		},
		filterDw2(info) {
			let that = this;
			let arrs = info.filter(item => item.msfs == that.selectMsxs);
			that.resultList = arrs;
			that.page = 1;
			that.maxPage = Math.ceil(that.resultList.length / 5);
			that.showResult = true;
			if (!that.userphone) {
				that.searchNum += 1;
				that.LogSetSearchtimes(that.searchNum);
			}
		},
		getFs(info) {
			let that = this;
			let danwei_code = info.bmdm;
			let zwmc = info.zwmc;
			uni.request({
				url: 'https://zw.offcn.com/zwapi/zhiwei/getzwlist?callback=?',
				method: 'get',
				dataType: 'jsonp',
				data: {
					zwcode: 'hlj',
					surl: '',
					page: 1,
					zwyear: '2022',
					danwei_code: danwei_code,
					limits: 30
				},
				success: function(res) {
					var resjson = JSON.parse(res.data.substr(1, res.data.length - 2));
					console.log(resjson);
					if (resjson.lists.length > 0) {
						that.fsList = resjson.lists;
						that.showFs = true;
					}
				}
			});
		},
		showToast(type, info) {
			this.$refs.uToast.show({
				type: type,
				icon: false,
				title: '失败主题',
				message: info
			});
		},
		prevPage() {
			if (this.page == 1) return;
			this.page--;
		},
		// 下一页
		nextPage() {
			if (this.page == this.maxPage) return;
			this.page++;
		},
		selectPage(index) {
			this.page = index;
		},
		imgView(imgUrl) {
			wx.previewImage({
				urls: [imgUrl], //需要预览的图片http链接列表，注意是数组
				current: '', // 当前显示图片的http链接，默认是第一个
				success: function(res) {},
				fail: function(res) {},
				complete: function(res) {}
			});
		}
	},
	components: { share }
};
</script>

<style>
@import url(./index.css);
</style>
